package milktea.servlet.user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import milktea.entity.USER;
import milktea.service.USERService;

/**
 * Servlet implementation class AdminLogin
 */
@WebServlet("/manage/adminlogin")
public class AdminLogin extends HttpServlet {
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setContentType("text/html;charset=gb2312");

        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");

        int count = USERService.selectByNM(userName, passWord);

        if (count > 0) {
            USER user = USERService.selectAdmin(userName, passWord);
            HttpSession session = request.getSession();
            session.setAttribute("name", user);
            session.setAttribute("isLogin", "1");

            if (user.getUSER_STATUS() == 2) {

                session.setAttribute("isAdminLogin", "1");
                response.sendRedirect("admin_index");

            } else {
                response.sendRedirect("/MilkTea1/index.jsp");


            }
        } else {
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户名或密码错误');");
            out.write("location.href='/MilkTea1/manage/login.jsp'");
            out.write("</script>");
        }

    }


}


